var wmp = new ActiveXObject("WMPlayer.OCX");
var wsettings, language = [], playlist, info = 0;
var windows = {
	player:0,
	playlist:0,
	preferences:0,
	about:0
	};

function ShowPlayer(){
	var PlusWnd = windows.player;
	if(!PlusWnd){
		PlusWnd = windows.player = CreateWnd("Player");
		if(wsettings.backimage)
			PlusWnd.ImageElmt_SetImageFile("bg", "\\" + wsettings.backimage);
		var wmpset = wmp.settings;
		PlusWnd.Button_SetCheckState("loop", wmpset.getMode("loop"));
		PlusWnd.Button_SetCheckState("shuffle", wmpset.getMode("shuffle"));
		PlusWnd.SendControlMessage("volume", 0x405, 1, wmpset.volume);
		PlusWnd.RegisterMessageNotification(0x114);
		PlusWnd.RegisterMessageNotification(0x233);
		SetAlpha(PlusWnd.handle, wsettings.playeralpha);
		OnEvent_Timer("info");
		OnEvent_Timer("data");
		SetButton();
		}
	Interop.Call("user32", "SwitchToThisWindow", PlusWnd.handle, 1);
	}

function ShowPlaylist(){
	var PlusWnd = windows.playlist;
	if(!PlusWnd){
		PlusWnd = windows.playlist = CreateWnd("Playlist");
		PlusWnd.RegisterMessageNotification(0x233);
		LoadPlaylist();
		SetAlpha(PlusWnd.handle, wsettings.playlistalpha);
		}
	Interop.Call("user32", "SwitchToThisWindow", PlusWnd.handle, 1);
	}

function ShowPreferences(){
	if(!windows.preferences){
		
		}
	}

function ShowAbout(){
	if(!windows.about){
		
		}
	}

function CreateWnd(wndid){
	return MsgPlus.CreateWnd("styles\\" + wsettings.style, wndid);
	}

function MediaAT(media){
	media = media||wmp.currentMedia;
	return media.getItemInfo("title") + " - " + media.getItemInfo("artist");
	}

function CachePlaylist(){
	var oPlaylist = wmp.currentPlaylist, n = oPlaylist.count;
	playlist = [];
	for(i=0;i<n;i++)
		playlist.push(oPlaylist.item(i));
	}

function LoadPlaylist(){
	var PlusWnd = windows.playlist, media, mediastr, search = new RegExp(PlusWnd.GetControlText("search"), "i");
	Interop.Call("user32", "SendMessageW", PlusWnd.GetControlHandle("playlist"), 0x1009, 0, 0);
	for(i in playlist){
		media = playlist[i];
		mediastr = MediaAT(media);
		if(!search.test(mediastr))
			continue;
		PlusWnd.LstView_AddItem("playlist", mediastr + " (" + (media.duration?media.durationString:"Stream") + ")", i*1);
		}
	}

function RemovePlaylist(item){
	var oPlaylist = wmp.currentPlaylist;
	if(typeof item == "undefined"){
		if(Interop.Call("user32", "MessageBoxW", windows.playlist.handle, language['PLAYLIST_WARNING'], "WLMini", 68) == 6)
			oPlaylist.clear();
		} else oPlaylist.removeItem(item);
	}

function SetButton(){
	var PlusWnd = windows.player;
	if(PlusWnd){
		var IsPlaying = wmp.playState == 3;
		Interop.Call("user32", "ShowWindow", PlusWnd.GetControlHandle("pause"), IsPlaying);
		Interop.Call("user32", "ShowWindow", PlusWnd.GetControlHandle("play"), !IsPlaying);
		}
	}

function LoadLanguage(lang){
	var file = new ActiveXObject("Scripting.FileSystemObject").OpenTextFile(MsgPlus.ScriptFilesPath + "\\langs\\" + lang), data;
	
	while(!file.AtEndOfStream){
		data = file.ReadLine().split(" = ");
		language[data[0]] = data[1];
		}
	
	file.Close();
	}

function wmpfunc(func){
	var wmpc = wmp.controls;
	switch(func){
		case 1:wmpc.stop();break;
		case 2:wmpc.pause();break;
		case 3:arguments[1]?wmpc.playItem(arguments[1]):wmpc.play();break;
		case 4:wmpc.next();break;
		case 5:wmpc.previous();break;
		case 6:case 7:
		var mode = func == 6?"loop":"shuffle";
		wmp.settings.setMode(mode, wsettings[mode] = !wmp.settings.getMode(mode));
		break;
		}
	}